package frame;

import type.GameMode;
import util.MultimediaIO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;

public class SettingPanel extends JPanel {
    public MainFrame frame;
    private JButton backButton; // 返回按钮
    private JButton developerOptionsButton; // 开发者选项按钮
    private JCheckBox soundCheckBox; // 音效开关
    private JComboBox<String> modeComboBox; // 模式选择

    public SettingPanel(MainFrame frame) {
        this.frame = frame;
        backButton = new JButton("返回");
        developerOptionsButton = new JButton("开发者选项");
        soundCheckBox = new JCheckBox("音效");
        modeComboBox = new JComboBox<>(new String[]{"选择模式(默认经典)", "经典模式", "糖豆人模式"});


        // 设置按钮坐标
        setLayout(null);
        developerOptionsButton.setBounds(260, 200, 280, 40);

        modeComboBox.setBounds(200, 260, 220, 40);
        soundCheckBox.setBounds(460, 260, 200, 40);

        backButton.setBounds(260, 320, 280, 40);

        //设置组件字体
        developerOptionsButton.setFont(new Font("Courier", Font.BOLD, 24));
        developerOptionsButton.setForeground(new Color(0, 0, 0));

        modeComboBox.setFont(new Font("Courier", Font.BOLD, 24));
        modeComboBox.setForeground(new Color(0, 0, 0));

        soundCheckBox.setFont(new Font("Courier", Font.BOLD, 24));
        soundCheckBox.setForeground(new Color(0, 0, 0));

        backButton.setFont(new Font("Courier", Font.BOLD, 24));
        backButton.setForeground(new Color(0, 0, 0));

        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 跳转回到主界面
                frame.setContentPane(new MainViewPanel(frame));
                frame.invalidate();
                frame.validate();
            }
        });

        developerOptionsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 跳转到开发者选项界面
                frame.setContentPane(new frame.DeveloperOptionsPanel(frame));
                frame.invalidate();
                frame.validate();
                frame.revalidate();
                frame.repaint();


            }
        });

        soundCheckBox.setSelected(MainFrame.soundCheckBox);
        soundCheckBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                // 打开或者关闭音效
                if (e.getStateChange() == ItemEvent.SELECTED&&e.getStateChange() == ItemEvent.SELECTED) {
                    frame.setSoundCheckBox(true);
                    System.out.println("已开启音效！！！");
                } else {
                    //打开音效
                    frame.setSoundCheckBox(false);
                    System.out.println("已关闭音效！！！");
                }
            }
        });
        modeComboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                // 根据选中项改变游戏模式
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    String mode = (String) e.getItem();
                    if ("经典模式".equals(mode)) {
                        // 切换到经典模式
                        frame.GAME_MODE = GameMode.CLASSIC;
                        System.out.println("已切换经典模式！！！");
                    } else if ("糖豆人模式".equals(mode)) {
                        // 切换到糖豆人模式
                        frame.GAME_MODE = GameMode.ADVANCED;
                        System.out.println("已切换糖豆人模式！！！");
                    } else if ("选择模式(默认经典)".equals(mode)) System.out.println("该选项无效！！！");
                }

            }
        });

        add(backButton);
        add(developerOptionsButton);
        add(soundCheckBox);
        add(modeComboBox);
    }
}
